import com.lmax.disruptor.*;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import org.junit.Test;

import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAdder;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * git地址

 https://gitee.com/yujianlong1988/java8andDisruptor.git
 * 日期类
 * @author yujianlong
 * @create 2018-06-16 11:29
 **/
public class DisruptorTest2 {

	Logger logger = Logger.getLogger("dis");



	public static class Tevent {

	}
	@Test
	public void test1() {//菱形
		new Object();
		final int DEFAULT_CORE_THREADS = Runtime.getRuntime().availableProcessors();
		ThreadFactory threadFactory = Executors.defaultThreadFactory();
		int bufferSize = 4;

		Disruptor<Tevent> disruptor =
				new Disruptor<Tevent>(Tevent::new, bufferSize, threadFactory, ProducerType.MULTI, new SleepingWaitStrategy());
		disruptor.handleExceptionsWith(new ExceptionHandler<Tevent>() {

			@Override
			public void handleEventException(Throwable throwable, long l, Tevent tevent) {
				throwable.printStackTrace();
				logger.log(Level.WARNING,"异常数据节点:{}", tevent);
				logger.log(Level.WARNING,"异常原因:{}", throwable.getMessage());
			}

			@Override
			public void handleOnStartException(Throwable throwable) {
				logger.log(Level.WARNING,"handleOnStartException:{}", throwable);
			}

			@Override
			public void handleOnShutdownException(Throwable throwable) {
				logger.log(Level.WARNING,"handleOnShutdownException:{}", throwable);
			}
		});

		EventHandler<Tevent> e0 = (Tevent tevent, long var2, boolean var4) -> {
			System.out.println("0");

		};

		EventHandler<Tevent> e11 = (Tevent tevent, long var2, boolean var4) -> {
			System.out.println("11");
		};


		EventHandler<Tevent> e2 = (Tevent tevent, long var2, boolean var4) -> {
			System.out.println("2");
		};

		EventHandler<Tevent> e3 = (Tevent tevent, long var2, boolean var4) -> {
			System.out.println("3");
		};
//		disruptor.handleEventsWith(e0).handleEventsWith(e11, e2).handleEventsWith(e3);
		disruptor.handleEventsWith(e11, e2).handleEventsWith(e0).handleEventsWith(e3);


		disruptor.start();
			/*定义单参发射器*/
		RingBuffer<Tevent> ringBuffer = disruptor.getRingBuffer();
		EventTranslatorOneArg<Tevent, Tevent> TRANSLATOR_ONE = (dayCutTransferEvent1, sequence, dayCutTransferEvent2) -> {

		};


		Tevent e1 = new Tevent();

		ringBuffer.publishEvent(TRANSLATOR_ONE, e1);
		disruptor.shutdown();
		long end = System.currentTimeMillis();
//		System.out.println("耗时=" + (end - start));
		logger.log(Level.WARNING,"异常数据节点:{0}", "aa");
		logger.log(Level.INFO,"异常数据节点1:{0}", "aa");

	}




}
